<template>
  <div class="pages personClass">
    <div class="pageLevel">
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <!-- <el-button type="primary" :disabled="dis" @click="goBack()" size="mini">返回</el-button> -->
        <!-- <el-button type="primary" :disabled="dis" @click="add()" size="mini">新增</el-button>
      <el-button type="primary" :disabled="dis" @click="deleteRows()" size="mini">删除</el-button> -->
      </div>
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="" label-width="15px">
            <el-input class="input" :disabled="dis" clearable v-model="selectdata.keyword" size="small" placeholder="输入分类名称或编号" @keyup.enter.native='searchSelect'
                      style='width: 250px'></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelect" :disabled="dis" class="Button">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" stripe style="width: 100%">
<!--          <el-table-column type="selection" width="55"></el-table-column>-->
          <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
          <el-table-column prop="code" label="分类编号" align="center">
            <template slot-scope="scope">
              <el-input v-show="scope.row.show==true" class="input" v-model="scope.row.code" placeholder="请输入分类编号" clearable size="small"></el-input>
              <span v-show="scope.row.show==false">{{scope.row.code}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="typeId" align="center" label="员工分类名称">
            <template slot-scope="scope">
              <el-input clearable v-show="scope.row.show==true" v-model="scope.row.name" placeholder="请输入会员分类名称" size="small"></el-input>
              <span v-show="scope.row.show==false">{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="typeId" align="center" label="描述">
            <template slot-scope="scope">
              <el-input clearable v-show="scope.row.show==true" v-model="scope.row.remark" placeholder="请输入描述" size="small"></el-input>
              <span v-show="scope.row.show==false">{{scope.row.remark}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="name" align="center" label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="small" :disabled="dis" v-show="scope.row.show==false" @click="handleEdit(scope.$index,scope.row.id)" type="text" class="addDetail">修改</el-button>
            <el-button size="small" v-show="scope.row.show==true" @click="save(scope.$index)" type="text" class="addDetail">保存</el-button>
            <el-button size="small" v-show="scope.row.show==true" @click="cancel(scope.$index)" type="text" class="addDetail">取消</el-button>
          </template>
        </el-table-column> -->
        </el-table>
        <!-- 页码 -->
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20,30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalItems">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import {
  findEmployeeTypeList,
  saveOrUpdateBrand,
  deleteBrand
} from "@/api";
export default {
  computed: mapState(["sub"]),
  components: {
  },
  data() {
    return {
      dis: false,
      selectdata: {
        keyword: ""
      },
      selectdatas: {
        keyword: ""
      },
      searchData: "",
      supplierType: [],
      tableData: [],
      multipleSelection: [],
      // 分页开始
      total: 0,
      pageSize: 20,
      totalItems: 0,
      currentPage: 1,
      ids: ""
    };
  },
  mounted() {
    // 获取表格数据
    this.tableget(20, 1);
  },
  methods: {
    // 获取表格数据
    tableget(pageSize, pageNum) {
      let data = {
        keyword: this.selectdatas.keyword,
        pageSize: pageSize,
        pageNum: pageNum
      };
      findEmployeeTypeList(data).then(res => {
        this.totalItems = res.data.pageTotal;
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i].show = false;
        }
        this.tableData = res.data.list;
      });
    },
    // 返回
    goBack() {
      window.history.go(-1);
    },
    // 新增-添加表的一行，直接填写
    add() {
      this.dis = true;
      let newele = {
        code: "",
        typeId: "",
        describe: "",
        show: true
      };
      this.tableData.unshift(newele);
    },
    // 保存-向后台提交新加的一条数据-？？是否要验证空字段
    save(i) {
      this.dis = false;
      this.tableData[i].show = false;
      let postdata = new FormData();
      postdata.append("code", this.tableData[i].code);
      postdata.append("name", this.tableData[i].name);
      postdata.append("id", this.ids);
      saveOrUpdateBrand(postdata).then(res => {
        this.tableget(20, 1);
        this.$message({
          type: "success",
          message: "添加成功!"
        });
      });
    },
    // 取消-将表格新加的一条数据删除
    cancel(i) {
      this.dis = false;
      this.tableData.shift();
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 删除
    deleteRows() {
      this.$confirm("确定要删除吗?", "提示", {
        center: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false,
        closeOnPressEscape: false
      }).then(() => {
        var arr = [];
        for (var i = 0; i < this.multipleSelection.length; i++) {
          arr.push(this.multipleSelection[i].id);
        }
        let data = {
          id: arr.toString()
        };
        deleteBrand(data).then(res => {
          this.tableget(20, 1);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      });
    },
    // 查询
    searchSelect() {
      this.selectdatas = this.selectdata;
      this.tableget(this.pageSize, 1);
    },
    //修改
    handleEdit(i, id) {
      this.ids = id;
      this.dis = true;
      this.tableData[i].show = true;
    },
    //跳页
    toPage() {
      this.tableget(this.pageSize, this.currentPage);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.tableget(this.pageSize, 1);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableget(this.pageSize, this.currentPage);
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
</style>

